package com.project.decoration.controller.price;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationPrice;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationPriceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 套餐controller
 * @date 2025/02/13 08:50
 */
@Controller
@ResponseBody
@RequestMapping("price")
public class DecorationPriceController {

    @Autowired
    private DecorationPriceService decorationPriceService;

    /** 分页获取套餐 */
    @PostMapping("getDecorationPricePage")
    public Result getDecorationPricePage(@RequestBody DecorationPrice decorationPrice) {
        Page<DecorationPrice> page = new Page<>(decorationPrice.getPageNumber(),decorationPrice.getPageSize());
        QueryWrapper<DecorationPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationPrice.getName()),DecorationPrice::getName,decorationPrice.getName())
                .eq(StringUtils.isNotBlank(decorationPrice.getArea()),DecorationPrice::getArea,decorationPrice.getArea())
                .like(StringUtils.isNotBlank(decorationPrice.getType()),DecorationPrice::getType,decorationPrice.getType())
                .like(StringUtils.isNotBlank(decorationPrice.getStyle()),DecorationPrice::getStyle,decorationPrice.getStyle())
                .like(StringUtils.isNotBlank(decorationPrice.getPlace()),DecorationPrice::getPlace,decorationPrice.getPlace())
                .eq(decorationPrice.getState() != null,DecorationPrice::getState,decorationPrice.getState())
                .orderByDesc(DecorationPrice::getCreateTime);
        Page<DecorationPrice> decorationPricePage = decorationPriceService.page(page, queryWrapper);
        return Result.success(decorationPricePage);
    }

    /** 根据id获取套餐 */
    @GetMapping("getDecorationPriceById")
    public Result getDecorationPriceById(@RequestParam("id")String id) {
        DecorationPrice decorationPrice = decorationPriceService.getById(id);
        return Result.success(decorationPrice);
    }

    /** 保存套餐 */
    @PostMapping("saveDecorationPrice")
    public Result saveDecorationPrice(@RequestBody DecorationPrice decorationPrice) {
        boolean save = decorationPriceService.save(decorationPrice);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑套餐 */
    @PostMapping("editDecorationPrice")
    public Result editDecorationPrice(@RequestBody DecorationPrice decorationPrice) {
        boolean save = decorationPriceService.updateById(decorationPrice);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除套餐 */
    @GetMapping("removeDecorationPrice")
    public Result removeDecorationPrice(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationPriceService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("套餐id不能为空！");
        }
    }

}
